<?php

/**
 * @author juanma
 * @copyright 2012
 */


class Sgit_Graficos_Proveedores extends Sgit_Graficos_Parent
{
    

     private function getData()
     {
        
        
        
        $this->key= $this->request->getParam('key','potencia');
        $this->id=  $this->request->getParam('id');
  

        switch($this->key){
            case'consumo':
                $this->y = 'kWh';
                $this->yaxistitle='kWh';
                $this->subtitle="Consumo kWh";
                $this->format='%d';
                break;
            case'potencia':
                $this->y = 'kW';
                $this->yaxistitle='kW';
                $this->subtitle="Potencia kW";
                $this->format='%d';
                break;
            case'potencia_max':
                $this->y = 'kW';
                $this->yaxistitle='kW';
                $this->subtitle="Potencia Máxima kW";
                $this->format='%d';
                break;
            case'importe':
                $this->y = '€';
                $this->yaxistitle='Euros';
                $this->subtitle="Importe Factura";
                $this->format='%u €';
                break;
            case'precio':
                $this->y = 'Euros x  kWh';
                $this->yaxistitle='Euros x  kWh';
                $this->subtitle="Precio Euros x kWh";
                $this->format='%01.4f €';
                break;
        }
        

        if(empty($this->start)){
            $this->start=new Zend_Date();
			$this->start->setDay(1);
			
            if($this->limit)
                $this->start->subMonth($this->limit);
            else
                $this->start->subMonth(1000);
        }else{
            $this->start=new Zend_Date($this->start,Zend_Date::ISO_8601);
        }
       
        $this->end = $this->end ? new Zend_Date($this->end,Zend_Date::ISO_8601) : new Zend_Date();


        $values['id_proveedor']=$this->id;
        $values['id_zona']=$this->id_zona;
        $values['limit']=$this->limit;
        
        

        
        $m_ProveedorConsumo= $this->loadModel('ProveedorConsumo');
         
        if($values['id_proveedor'])
            $filas = $m_ProveedorConsumo->getConsumos($values);
        elseif($values['id_zona'])
            $filas = $m_ProveedorConsumo->getConsumosGroup($values);
        

        $filas=array_reverse($filas);
        
        $this->title='Factura Compañia: '.Sgit_Cache::single()->model('Datacenter')->getKey($filas[0]['id_zona']).' '. $filas[0]['proveedor'];

        $i=0;
        
        $fecha=new Zend_Date();
        $fecha->setDay(1);
		
        foreach($filas as $v)
        {
            $fecha->setYear($v['anno'])->setMonth($v['mes'])->addMonth(1);
            
            $this->label[$i]=ucfirst($fecha->get('MMM-yy'));
            $this->labelJsonFormat[$i]=ucfirst($fecha->get('MMM-yyyy'));
            $this->labelJson[$i]=$fecha->get('yyyy-MM-dd');
            
            

            $this->value['potencia'][$i]     = $this->setValue($v['potencia']);
            $this->value['potencia_max'][$i] = $this->setValue($v['potencia_max']);         
            $this->value['consumo'][$i]=       $this->setValue($v['consumo']);
            $this->value['importe'][$i]=       $this->setValue($v['importe']);
            $this->value['precio'][$i]=        $this->setValue($v['precio']);
             
            $this->text1[]=$this->label[$i];
           	$y2=($this->value[$this->key][$i]) ? " $y" : "";
                	
            if($this->key=='precio') {
                $this->text2[]=$this->view->numES($this->value[$this->key][$i],4).$y2;
            } else{
                $this->text2[]=$this->view->numES($this->value[$this->key][$i],0).$y2;
            }
                                
            if($this->key=='potencia') {
                $this->text3[]=$this->view->numES($this->value['potencia_max'][$i],0).$y2;
            }     
             
            $i++;

        }
    

    }


     public function jpGrapgh()
     {
       
        $this->getData();

        $this->loadJpgraph('bar'); 
        $this->loadJpgraph('line'); 
        
        $this->text1=array_reverse($this->text1);
        $this->text1 = array_slice($this->text1, 0,floor($this->width/40));
        $this->text1=implode(PHP_EOL,$this->text1);
            
        $this->text2=array_reverse($this->text2);
        $this->text2 = array_slice($this->text2, 0, floor($this->width/40));
        $this->text2=implode(PHP_EOL,$this->text2);
            

        $step=$this->_step(count($this->value[$this->key]),$this->width);    
        $nombre=md5($this->key.$this->width.$this->height.serialize($this->value[$this->key]));


        $graph = $this->_graph($this->width,$this->height);
        $graph->SetMargin(75, 75, 30, 80);

        $graph->title->Set(utf8_decode($this->title));
        $graph->subtitle->Set(utf8_decode($this->subtitle));
        $graph->yaxis->SetTitle($this->yaxistitle,'middle'); 
        $graph->xaxis->SetTickLabels($this->label);
        $graph->xaxis->SetTextLabelInterval($step);
        $graph->xaxis->SetLabelAngle(90);


        if($this->key=='precio')
        {
            $graph->yaxis->SetLabelFormatCallback(array($this,'_yLabelFormat'));
        }
 
     
        $barplot= $this->_barPlot($this->value[$this->key],'#c0c0c0');
        $barplot->value->Show();
        $barplot->value->SetFont(FF_ARIAL,FS_BOLD,8);
        $barplot->value->SetAngle(90);
        $barplot->value->SetFormat($this->format);     
        $barplot->SetValuePos('center');
        $barplot->SetFillGradient( $this->color.'@0.3', $this->color.'@0.6',GRAD_VERT);
        $barplot->SetWidth(1);
        $barplot->SetValuePos('center');
         
        $graph->Add($barplot);
    

 

        if($this->key=='potencia'){
                       
            $lineplot4= $this->_linePlot($this->value['potencia_max'],"Maximetro",'red',2,false);
            $lineplot4->SetBarCenter();
            $graph->Add($lineplot4);
                        
        }
          


        $graph->footer->left->Set(utf8_decode("Fuente: Factura de Compañia Eléctrica"));
        $graph->Stroke();
        
        
     }
     
     public function googleVisualization(&$view)
     {
        
        $this->limit=null;
        $this->getData();
        
        $view->title=$this->title.' '.$this->subtitle;
        $view->subtitle=$this->subtitle;
        $view->options="
                'title': '{$view->title}',
                'hAxis': {'slantedText': true,slantedTextAngle:90},
                'legend': {'position': 'right'},
                'animation' : {'duration' : 2000},
                'curveType' : 'function',
                'vAxis' : {'title': 'pue'},
                 strictFirstColumnType: false,
                'vAxis' : {'minValue': 0, title: '$this->yaxistitle'},
                 colors:['green'],
        ";
        $view->y=$this->y;
        $view->label=$this->labelJson;
        $view->label_format=$this->labelJsonFormat;
        $view->data=$this->value[$this->key];

       
     }
     

    
}